//
//  ShopCategoryView.m
//  跨界收
//
//  Created by Allen on 2018/11/11.
//  Copyright © 2018年 gztx. All rights reserved.
//

#import "ShopCategoryView.h"

@implementation ShopCategoryView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self creatCategoryView];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
- (void)creatCategoryView
{
    _titleArray = @[@"企业经营",@"个体工商户",@"个人"];
    _categoryArray = @[@"持有责任公司执照的商户",@"持个人工商营业执照的商户",@"暂无营业执照的自然人"];
    CGFloat distance = 10;
    for (NSInteger i=0; i<_titleArray.count; i++) {
        CGFloat Y = distance+i*80;
        UIView *categoryView = [self sigleCategoryView:i];
        categoryView.frame = CGRectMake(15, Y, SCREEN_WIDTH-30, 80);
        categoryView.tag = 1000+i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapView:)];
        [categoryView addGestureRecognizer:tap];
        [self addSubview:categoryView];
    }
}
- (void)tapView:(UITapGestureRecognizer *)tap
{
    UIView *tapView = (UIView *)tap.view;
    if (self.selectBlock) {
        self.selectBlock(tapView.tag);
    }
    for (id view in self.subviews) {
        if ([view isKindOfClass:[UIView class]]) {
            UIView *tView = (UIView *)view;
            for (id bg in tView.subviews) {
                if ([bg isKindOfClass:[UIImageView class]]) {
                    UIImageView *bgIV = (UIImageView *)bg;
                    if (tView.tag == tapView.tag) {
                        if (bgIV.tag-500 <0) {
                            if (bgIV.tag-100 == tapView.tag-1000) {
                                bgIV.layer.masksToBounds = YES;
                                bgIV.layer.borderWidth = 1;
                                bgIV.layer.borderColor = [[UIColor cz_colorWithHex:0x239fdb] CGColor];
                            }else{
                                bgIV.layer.masksToBounds = YES;
                                bgIV.layer.borderWidth = 1;
                                bgIV.layer.borderColor = [[UIColor lightGrayColor] CGColor];
                            }
                        }else{
                            if (bgIV.tag-500 == tapView.tag-1000) {
                                bgIV.hidden = NO;
                            }else{
                                bgIV.hidden = YES;
                            }
                        }
                        
                    }else{
                        if (bgIV.tag-500 <0) {
                            bgIV.layer.masksToBounds = YES;
                            bgIV.layer.borderWidth = 1;
                            bgIV.layer.borderColor = [[UIColor lightGrayColor] CGColor];
                            
                        }else{
                            bgIV.hidden = YES;
                        }
                    }
                }
                if ([bg isKindOfClass:[UILabel class]]) {
                    UILabel *label = (UILabel *)bg;
                    if (tView.tag == tapView.tag) {
                        label.textColor = [UIColor cz_colorWithHex:0x239fdb];
                    }else{
                        if (label.tag-300 <0) {
                            label.textColor = [UIColor blackColor];
                        }else{
                            label.textColor = [UIColor lightGrayColor];
                        }
                    }
                }
            }
        }
    }
}
- (UIView *)sigleCategoryView:(NSInteger )tag
{
    UIView *categoryView = [[UIView alloc] initWithFrame:CGRectMake(15.0, 0.0, SCREEN_WIDTH-30.0, 80.0)];
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH-30.0, 70.0)];
    bgIV.tag = 100+tag;
    [categoryView addSubview:bgIV];
    //    selected
    UIImageView *selectedIV = [[UIImageView alloc] initWithFrame:CGRectMake(categoryView.width-28.5, 10,28.5, 28.5)];
    selectedIV.tag = 500+tag;
    selectedIV.image = [UIImage imageNamed:@"shang_gou"];
    [categoryView addSubview:selectedIV];
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 20, SCREEN_WIDTH-30.0, 25.0)];
    titleLabel.tag = 200+tag;
    titleLabel.text = _titleArray[tag];
    titleLabel.font = [UIFont systemFontOfSize:16.0];
    [categoryView addSubview:titleLabel];
    
    UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 45.0, SCREEN_WIDTH-30.0, 25.0)];
    categoryLabel.tag = 300+tag;
    categoryLabel.font = [UIFont systemFontOfSize:15.0];
    categoryLabel.text = _categoryArray[tag];
    [categoryView addSubview:categoryLabel];
    
    if (tag == 0) {
        bgIV.layer.masksToBounds = YES;
        bgIV.layer.borderWidth = 1;
        bgIV.layer.borderColor = [[UIColor cz_colorWithHex:0x239fdb] CGColor];
        selectedIV.hidden = NO;
        titleLabel.textColor = [UIColor cz_colorWithHex:0x239fdb];
        categoryLabel.textColor = [UIColor cz_colorWithHex:0x239fdb];
    }else{
        bgIV.layer.masksToBounds = YES;
        bgIV.layer.borderWidth = 1;
        bgIV.layer.borderColor = [[UIColor lightGrayColor] CGColor];
        selectedIV.hidden = YES;
        titleLabel.textColor = [UIColor blackColor];
        categoryLabel.textColor = [UIColor lightGrayColor];
    }
    
    
    return categoryView;
}
@end
